<?php

/**
 * This is the model class for table "data_clip".
 *
 * The followings are the available columns in table 'data_clip':
 * @property string $id
 * @property string $name
 * @property string $image
 * @property string $clip_url
 * @property string $introtext
 * @property string $description
 * @property string $cid
 * @property integer $hot
 * @property integer $global_hot
 * @property string $order
 * @property string $global_order
 * @property string $createddate
 * @property integer $active
 */
class Clip extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Clip the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'data_clip';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, cid, clip_url,createddate', 'required'),
			array('hot, global_hot, active', 'numerical', 'integerOnly'=>true),
			array('name, clip_url', 'length', 'max'=>300),
			array('image', 'length', 'max'=>200),
			array('introtext,tags', 'length', 'max'=>500),
			array('cid, order, global_order,price', 'length', 'max'=>20),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, clip_url, introtext, description, cid, hot, global_hot, order, global_order, createddate, active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'clipcategory'=>array(self::MANY_MANY,'Clipcategory','data_clip(id,cid)','select'=>'id,name'),
				'retail'=>array(self::MANY_MANY,'Package','data_clip(id,price)','select'=>'id,name,price'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'image' => 'Image',
			'clip_url' => 'Clip Url',
			'introtext' => 'Introtext',
			'description' => 'Description',
			'retail'=>'Price',
			'cid' => 'Cid',
			'hot' => 'Hot',
			'global_hot' => 'Global Hot',
			'order' => 'Order',
			'global_order' => 'Global Order',
			'createddate' => 'Createddate',
			'active' => 'Active',
			'clipcategory'=>'Category',
			'tags'=>'Tags'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('clip_url',$this->clip_url,true);
		$criteria->compare('introtext',$this->introtext,true);
		$criteria->compare('description',$this->description,true);
		if($this->cid!=0)
			$criteria->compare('cid',$this->cid);
		if($this->hot!='')
			$criteria->compare('hot',$this->hot);
		if($this->global_hot!='')
			$criteria->compare('global_hot',$this->global_hot);
		$criteria->compare('order',$this->order,true);
		$criteria->compare('global_order',$this->global_order);
		$criteria->compare('createddate',$this->createddate,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
		        'defaultOrder'=>'t.id DESC',
		    ),
		));
	}
}